﻿#region Copyright
// <copyright file="MvxViewModelLoader.cs" company="Cirrious">
// (c) Copyright Cirrious. http://www.cirrious.com
// This source is subject to the Microsoft Public License (Ms-PL)
// Please see license.txt on http://opensource.org/licenses/ms-pl.html
// All other rights reserved.
// </copyright>
// 
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com
#endregion

using Cirrious.MvvmCross.Application;
using Cirrious.MvvmCross.Exceptions;
using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Application;
using Cirrious.MvvmCross.Interfaces.ServiceProvider;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.Views;
using TeaLightV5.Foundation.MvvmCross.Interfaces.ViewModels;
using TeaLightV5.Foundation.MvvmCross.Views;

namespace TeaLightV5.Foundation.MvvmCross.Application
{
    // DJJ changed 
    public class TeaViewModelLoader : IMvxViewModelLoader
          , IMvxServiceConsumer

    {
        #region IMvxViewModelLoader Members

        private MvxViewModelLoader _loader;

        public IMvxViewModel LoadViewModel(MvxShowViewModelRequest request)
        {
            TeaShowViewModelRequest teaRequest = request as TeaShowViewModelRequest;
            if (teaRequest != null)
            {
                return LoadViewModel(teaRequest);
            }
            if (_loader == null)
            {
                _loader = new MvxViewModelLoader();
            }
            return _loader.LoadViewModel(request);
        }


        public IMvxViewModel LoadViewModel(TeaShowViewModelRequest request)
        {
            if (request.ViewModelType == typeof(MvxNullViewModel))
                return new MvxNullViewModel();

            var viewModelLocatorFinder = this.GetService<IMvxViewModelLocatorFinder>();
            var viewModelLocator = viewModelLocatorFinder.FindLocator(request) as ITeaViewModelLocator ;

            if (viewModelLocator == null)
            {
                viewModelLocator = new TeaViewModelLocator();
            }
                //throw new MvxException("Sorry - somehow there's no viewmodel locator registered for {0}",
                //                       request.ViewModelType);

            IMvxViewModel model = null;
            if (request.ModelParameters == null)
            {
                if (!viewModelLocator.TryLoad(request.ViewModelType, request.ParameterValues, out model))
                {
                    throw new MvxException(
                        "Failed to load ViewModel for type {0} from locator {1}",
                        request.ViewModelType, viewModelLocator.GetType().Name);
                }
            }
            else
            {
                if (!viewModelLocator.TryLoad(request.ViewModelType, request.ModelParameters, out model))
                {
                    throw new MvxException(
                        "Failed to load ViewModel for type {0} from locator {1}",
                        request.ViewModelType, viewModelLocator.GetType().Name);
                }
            }
            if (model != null)
                model.RequestedBy = request.RequestedBy;

            return model;
        }

        #endregion

    }
}